package com.minelx.table.core;

import com.minelx.table.core.series.Series;

import java.util.List;

import static java.util.stream.Collectors.joining;

public class Table {

	public static final int HEAD_LENGTH = 5;

	private final TableProvider tableProvider;

	Table(TableProvider tableProvider) {
		this.tableProvider = tableProvider;
	}

	public long width() {
		return form().columnIndexes().len();
	}

	public long height() {
		return form().rowIndexes().len();
	}

	public Series<String> column(String indexName) {
		return form().column(form().title().indexOf(indexName));
	}

	public Table drop(List<String> columns) {
		return Tables.columnIndexed(this, form().title().mapSource(column -> !columns.contains(column)));
	}

	public Table rows(Series<Boolean> booleanIndexes) {
		return Tables.rowIndexed(this, booleanIndexes);
	}

	public String toText() {
		return form().title().stream().collect(joining(",")) +
				"\n" +
				form().rowIndexes().stream()
						.mapToObj(at -> form().row(at))
						.map(row -> row.stream().collect(joining(",")))
						.collect(joining("\n"));
	}

	public Table head() {
		// FIXME 2021/8/14  wait for me!!!     head remains all rows
		return rows(Series.fill(Boolean.TRUE, form().rowIndexes()));
	}

	public TableProvider form() {
		return tableProvider;
	}
}
